home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / bbs / pad321.zip / COLOUR.MH < prev    next >
Text File  |  1996-05-13  |  7KB  |  202 lines

  1. #ifndef __COLOUR_MH
  2. #define __COLOUR_MH
  3.  
  4. // Function list:
  5. //
  6. // int          nameToColour            (string: colourName);
  7. // string       getColourString         (int: fg, int: bg);
  8. // string       stringToColourString    (string: inputString);
  9. // void         changeColour            (int: fg, int: bg);
  10. // string       outString               (string: inputString);
  11. // void         colourInit              ();
  12. // bool         colourToken             (string: token, string: params);
  13. //
  14.  
  15. // Colour constants, to be used with the colour functions below
  16.  
  17. #define C_BLACK               0
  18. #define C_BLUE                1
  19. #define C_GREEN               2
  20. #define C_CYAN                3
  21. #define C_RED                 4
  22. #define C_MAGENTA             5
  23. #define C_BROWN               6
  24. #define C_GRAY                7
  25. #define C_DKGRAY              8
  26. #define C_LBLUE               9
  27. #define C_LGREEN              10
  28. #define C_LCYAN               11
  29. #define C_LRED                12
  30. #define C_LMAGENTA            13
  31. #define C_YELLOW              14
  32. #define C_WHITE               15
  33.  
  34. // int nameToColour (string: colourName)
  35. //
  36. //      Converts a string containing the name of a colour into an int equal
  37. //      to one of the C_* colour constants, below. These colour constants
  38. //      may be used with the getColourString and changeColour functions.
  39.  
  40. int nameToColour (string: colourName) {
  41.   colourName := strupper (colourName);
  42.  
  43.   if (colourName = "BLACK")             return 0;
  44.   if (colourName = "BLUE")              return 1;
  45.   if (colourName = "GREEN")             return 2;
  46.   if (colourName = "CYAN")              return 3;
  47.   if (colourName = "RED")               return 4;
  48.   if (colourName = "MAGENTA")           return 5;
  49.   if (colourName = "BROWN")             return 6;
  50.   if (colourName = "GRAY")              return 7;
  51.   if ((colourName = "DKGRAY")
  52.     or (colourName = "DARKGRAY"))       return 8;
  53.   if ((colourName = "LBLUE")
  54.     or (colourName = "LIGHTBLUE"))      return 9;
  55.   if ((colourName = "LGREEN")
  56.     or (colourName = "LIGHTGREEN"))     return 10;
  57.   if ((colourName = "LCYAN")
  58.     or (colourName = "LIGHTCYAN"))      return 11;
  59.   if ((colourName = "LRED") 
  60.     or (colourName = "LIGHTRED"))       return 12;
  61.   if ((colourName = "LMAGENTA")
  62.     or (colourName = "LIGHTMAGENTA"))   return 13;
  63.   if (colourName = "YELLOW")            return 14;
  64.   if (colourName = "WHITE")             return 15;
  65.   return -1;
  66.   }
  67.  
  68. // string getColourString (int: fgCol, int: bgCol)
  69. //
  70. //      Given a foreground and background colour (equal to one of the
  71. //      C_* colour constants), this function returns a string which,
  72. //      when printed, will change the text colour to that colour.
  73.  
  74. string getColourString (int: fgCol, int: bgCol) {
  75.   string: result;
  76.   if (fgCol = -1) return "";
  77.   if (bgCol = -1) bgCol := 0;
  78.   result := "\x16\x01";
  79.   result [3] := fgCol + 16*bgCol;
  80.   return result;
  81.   }
  82.  
  83. // string stringToColourString (string: s);
  84. //
  85. //      Converts a mecca-like colour command into an AVATAR colour code. When this
  86. //      colour code is printed, it will change the screen colour.
  87. //
  88. //      Examples of mecca-like colour commands handled by this function:
  89. //
  90. //      blue on black
  91. //      yellow
  92. //      yellow ON blue
  93. //
  94. //      If the string "on" ommitted, the background colour is assumed to be black.
  95. //
  96.  
  97. string stringToColourString (string: s) {
  98.   int: pos, fg;
  99.   string: sub;
  100.  
  101.   pos := 0;
  102.   sub := strtok (s," \t", pos);
  103.   if (sub <> "") fg := nameToColour (sub);
  104.   sub := strupper (strtok (s," \t", pos));
  105.   if (sub <> "ON") return getColourString (fg, 0);
  106.   sub := strtok (s, " \t", pos);
  107.   return getColourString (fg, nameToColour (sub));
  108.   }
  109.  
  110. // void changeColour (int: fgCol, int: bgCol)
  111. //
  112. //      Changes the text colour, given a C_* constant for the new foreground
  113. //      and background colours.
  114.  
  115. void changeColour (int: fgCol, int: bgCol) {
  116.   print (getColourString (fgCol, bgCol));
  117.   }
  118.  
  119. // Converts a string containing mecca colour commands and Mex escape codes
  120. // into a string that, when displayed, will actually show the correct colours
  121. // and special characters.
  122. //
  123. // For example,
  124. //
  125. //      [yellow on blue]Hello [green]world\n[white ]This is a test\n
  126. //
  127. // Would be converted into a string which, when displayed, would appear as:
  128. //
  129. //      Hello world
  130. //      This is a test
  131. //
  132. // The string "Hello" would be yellow on a blue background, the string "world"
  133. // would be green on black, and the string "This is a test" would be white.
  134. //
  135. // This is useful for configuring colours and output strings from input files.
  136. //
  137.  
  138. string outString (string: src) {
  139.   int: sidx, tidx, slen, cmd, tmp1, tmp2;
  140.   string: result;
  141.  
  142.   slen := strlen (src);
  143.   tidx := 1;
  144.  
  145.   for (sidx := 1; sidx <= slen; sidx := sidx + 1) {
  146.     if ((src [sidx] = '\\') and (sidx < slen)) {
  147.       sidx := sidx + 1;
  148.       cmd := src [sidx];
  149.  
  150.                 if (cmd = '\\') {result [tidx] := '\\';}
  151.       else      if (cmd = 'n')  {result [tidx] := '\n';}
  152.       else      if (cmd = 'r')  {result [tidx] := '\r';}
  153.       else      if (cmd = 'a')  {result [tidx] := '\a';}
  154.       else      if (cmd = 'b')  {result [tidx] := '\b';}
  155.       else      if (cmd = 'f')  {result [tidx] := '\f';}
  156.       else      if (cmd = '\'') {result [tidx] := '\'';}
  157.       else      if (cmd = '[')  {result [tidx] := '[';}
  158.       else      if (cmd = '\"') {result [tidx] := '\"';}
  159.       else      if ((cmd = 'x') and (sidx < slen + 2)) {
  160.         tmp1 := hexDig (src [sidx+1]);
  161.         tmp2 := hexDig (src [sidx+2]);
  162.         if ((tmp1 > -1) and (tmp2 > -1)) {
  163.           result [tidx] := 16 * tmp1 + tmp2;
  164.           };
  165.         sidx := sidx + 2;
  166.         };
  167.  
  168.       tidx := tidx + 1;
  169.       }
  170.     else if (src [sidx] = '[') {
  171.       sidx := sidx + 1;
  172.       if (src [sidx] = '[') {
  173.         result [tidx] := '[';
  174.         tidx := tidx + 1;
  175.         }
  176.       else {
  177.         tmp1 := stridx (src, sidx, ']');
  178.         result := result + stringToColourString (
  179.           strtrim (substr (src, sidx, tmp1 - sidx), " \t"));
  180.         tidx := strlen (result) + 1;
  181.         sidx := tmp1;
  182.         };
  183.       }
  184.     else {
  185.       result [tidx] := src [sidx];
  186.       tidx := tidx + 1;
  187.       };
  188.     };
  189.   return result;
  190.   }
  191.  
  192. void colourInit () {
  193.   // Nothing to do!
  194.   }
  195.  
  196. bool colourToken (string: token, string: params) {
  197.   // Nothing to do!
  198.   return False;
  199.   }
  200.  
  201. #endif
  202.